let mongoose = require("mongoose");

// 添加更详细的连接日志
mongoose.connection.on("connected", () => {
  console.log("MongoDB connected successfully");
});

mongoose.connection.on("error", (err) => {
  console.log("MongoDB connection error:", err);
});

mongoose.connection.on("disconnected", () => {
  console.log("MongoDB disconnected");
});

mongoose
  .connect(
    "mongodb+srv://2264521353:1234567890@six0.hic1spu.mongodb.net/2408B",
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
  )
  .then(() => {
    console.log("数据库连接成功");
  })
  .catch((err) => {
    console.log("数据库连接失败:", err);
  });

// 用户表
let User = new mongoose.Schema({
  userName: String,
  passWord: String,
  img: String,
  phone: String,
  money: {
    type: Number,
    default: 0,
  },
  concern: {
    type: Array,
    default: [],
  },
  sex: String,
  age: Number,
    // 是否是vip
    vip:{
      type:Number,
      default:0
    }
});

// 动态表
let dynamics = new mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    time: {
        type: Date,
        default: Date.now
    },
    img: {
        type: String,
        default: ''
    },
    media_type: {
        type: String,
        enum: ['image', 'video'],
        default: 'image'
    },
    address: {
        type: String,
        default: ''
    },
    comment: {
        type: Array,
        default: []
    },
    like: {
        type: Array,
        default: []
    },
    user_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    }
});


let Dynamics = mongoose.model("Dynamics", dynamics);
let UserModel = mongoose.model("User", User);

module.exports = { UserModel, Dynamics };
